import 'package:flutter/cupertino.dart';
import 'package:lib_bt/BleManager.dart';
import 'package:lib_bt/send/BleSendManager.dart';
import 'package:lib_bt/send/SendBean.dart';
import 'package:lib_music/lib_mic.dart';
import 'package:lib_music/lib_music.dart';
import 'package:lib_music/listener/OnFftDataListener.dart';
import 'package:lib_music/listener/OnMicCallback.dart';
import 'package:lib_music/listener/OnMusicCallback.dart';

import '../../ble/protocol/demoble/UUIDConfig.dart';
import '../../ble/protocol/iledcolor/send/ColorPackageImpl.dart';

class MusicDataSendUtil extends ChangeNotifier implements OnMusicCallback, OnFftDataListener, OnMicCallback {
  ///发送给屏幕的模式
  int _effects = 0;
  int lastTime = 0;

  bool rhythmEnable = false;

  int get effects {
    return _effects;
  }

  set effects(int value) {
    _effects = value;
    notifyListeners();
  }

  static MusicDataSendUtil? _instance;

  static MusicDataSendUtil get instance => _instance ??= MusicDataSendUtil._();

  MusicDataSendUtil._() {
    LibMusic.instance.addOnMusicCallback(this);
    LibMusic.instance.addOnFftDataListener(this);
    LibMic.instance.addOnMicCallback(this);
    LibMic.instance.addOnFftDataListener(this);
  }

  @override
  void onFftDataCapture(List<int> data) {
    /*if (LibMusic.instance.isPlaying) {
      BleBean bleBean = BleBean(bean: AutoConnectUtil.instance.getLastConnectBleBean());
      var sendData = LibMusic.instance.readyData(data, bleBean.height, 1);
      //截取前面12个元素
      sendData = sendData.sublist(0, 12);
      GlobalPropertiesManager.instance.setRhythmData(0, sendData);
    }*/
    //print("onFftDataCapture.....................");
    if ((DateTime.now().millisecondsSinceEpoch - lastTime) > 100) {
      lastTime = DateTime.now().millisecondsSinceEpoch;
      setRhythmData(effects, data);
    }
    notifyListeners();
  }

  /// 律动的数据发送
  void setRhythmData(int type, List<int> rhyData) {
    if (!rhythmEnable) {
      return;
    }

    var bleList = BleManager.instance.getConnectBleBeans();
    if (bleList.isEmpty) {
      return;
    }

    rhyData = [1, type] + rhyData;

    var package = ColorPackageImpl.createSimplePackage(
      [0x54, 0x04],
      rhyData,
      characteristicUuid: UUIDConfig.instance.uuid_write_2,
    );
    List<SendBean> sendBeans = [];
    for (var bleBean in bleList) {
      SendBean sendBean = SendBean()
        ..address = bleBean.address
        ..hasResponse = false
        ..addPackage(package);
      sendBeans.add(sendBean);
    }
    print("发送数据,当前时间：${DateTime.now().millisecondsSinceEpoch}");
    BleSendManager.instance.send(sendBeans);
  }

  /// 设置律动的开关
  ///
  /// @param enable
  void setRhythmEnable(bool enable) {
    rhythmEnable = enable;
    var bleList = BleManager.instance.getConnectBleBeans();
    if (bleList.isEmpty) {
      return;
    }

    if (!enable) {
      var rhyData = [0, 1];
      var package = ColorPackageImpl.createSimplePackage(
        [0x54, 0x04],
        rhyData,
        characteristicUuid: UUIDConfig.instance.uuid_write_2,
      );
      List<SendBean> sendBeans = [];
      for (var bleBean in bleList) {
        SendBean sendBean = SendBean()
          ..address = bleBean.address
          ..hasResponse = false
          ..addPackage(package);
        sendBeans.add(sendBean);
      }
      BleSendManager.instance.send(sendBeans);
    }
  }

  @override
  void onPausePlay() {}

  @override
  void onPlayComplete() {}

  @override
  void onProgress(int progress) {}

  @override
  void onStartPlay() {
    print("onStartPlay.....................");
    setRhythmEnable(true);
  }

  @override
  void onStopPlay() {
    print("onStopPlay.....................${DateTime.now().millisecondsSinceEpoch}");
    setRhythmEnable(false);
  }

  @override
  void onWaveFormDataCapture(List<int> data) {}
}
